home *** CD-ROM | disk | FTP | other *** search
/ IRIX Base Documentation 2002 November / SGI IRIX Base Documentation 2002 November.iso / usr / share / catman / u_man / cat3 / Tcl / memory.z / memory
Encoding:
Text File  |  2002-10-03  |  17.0 KB  |  331 lines

  1.  
  2.  
  3.  
  4. MMMMeeeemmmmoooorrrryyyy((((3333TTTTccccllll))))                                                      MMMMeeeemmmmoooorrrryyyy((((3333TTTTccccllll))))
  5.  
  6.  
  7.  
  8. NNNNAAAAMMMMEEEE
  9.      ckalloc, memory, ckfree, Tcl_DisplayMemory, Tcl_InitMemory,
  10.      Tcl_ValidateAllMemory - Validated memory allocation interface.
  11.  
  12. SSSSYYYYNNNNOOOOPPPPSSSSIIIISSSS
  13.      mmmmeeeemmmmoooorrrryyyy iiiinnnnffffoooo
  14.  
  15.      mmmmeeeemmmmoooorrrryyyy ttttrrrraaaacccceeee [oooonnnn||||ooooffffffff]
  16.  
  17.      mmmmeeeemmmmoooorrrryyyy vvvvaaaalllliiiiddddaaaatttteeee [oooonnnn||||ooooffffffff]
  18.  
  19.      mmmmeeeemmmmoooorrrryyyy ttttrrrraaaacccceeee____oooonnnn____aaaatttt____mmmmaaaalllllllloooocccc _n_n_n
  20.  
  21.      mmmmeeeemmmmoooorrrryyyy bbbbrrrreeeeaaaakkkk____oooonnnn____mmmmaaaalllllllloooocccc _n_n_n
  22.  
  23.      mmmmeeeemmmmoooorrrryyyy ddddiiiissssppppllllaaaayyyy _f_i_l_e
  24.  
  25.  
  26.  
  27.      #include <tcl.h>
  28.  
  29.      char *
  30.      ckalloc (unsigned size)
  31.  
  32.      void
  33.      ckfree (char *ptr)
  34.  
  35.      int
  36.      Tcl_DumpActiveMemory (char *fileName);
  37.  
  38.      void
  39.      Tcl_ValidateAllMemory (char *file,
  40.                             int   line)
  41.  
  42.      void
  43.      Tcl_InitMemory (interp)
  44.  
  45. AAAARRRRGGGGUUUUMMMMEEEENNNNTTTTSSSS
  46.      A pointer to the Tcl interpreter.  The filename of the caller of
  47.      Tcl_ValidateAllMemory.  The line number of the caller of
  48.      Tcl_ValidateAllMemory.  File to display list of active memory.
  49.  
  50.  
  51. DDDDEEEESSSSCCCCRRRRIIIIPPPPTTTTIIIIOOOONNNN
  52.    cccckkkkaaaalllllllloooocccc
  53.      Thi macro allocates memory, in the same manner as mmmmaaaalllllllloooocccc, with the
  54.      following differences: One, cccckkkkaaaalllllllloooocccc checks the value returned from mmmmaaaalllllllloooocccc
  55.      (it calls mmmmaaaalllllllloooocccc for you) and panics if the allocation request fails.
  56.      Two, if enabled at compile time, a version of cccckkkkaaaalllllllloooocccc with special memory
  57.      debugging capabilities replaces the normal version of cccckkkkaaaalllllllloooocccc, which aids
  58.      in detecting memory overwrites and leaks (repeated allocations not
  59.      matched by corresponding frees).
  60.  
  61.  
  62.  
  63.                                                                         PPPPaaaaggggeeee 1111
  64.  
  65.  
  66.  
  67.  
  68.  
  69.  
  70. MMMMeeeemmmmoooorrrryyyy((((3333TTTTccccllll))))                                                      MMMMeeeemmmmoooorrrryyyy((((3333TTTTccccllll))))
  71.  
  72.  
  73.  
  74.      Parameters:
  75.        oooo _s_i_z_e - The size of the memory block to be allocated.
  76.  
  77.      Returns:
  78.        A pointer to the allocated memory block.
  79.  
  80.    cccckkkkffffrrrreeeeeeee
  81.      This macro frees memory allocated by cccckkkkaaaalllllllloooocccc.  Like cccckkkkaaaalllllllloooocccc, when memory
  82.      debugging is enabled, cccckkkkffffrrrreeeeeeee has enhanced capabilities for detecting
  83.      memory overwrites and leaks.
  84.  
  85.      It is very important that you use cccckkkkaaaalllllllloooocccc when you need to allocate
  86.      memory, and that you use cccckkkkffffrrrreeeeeeee to free it.  Should you use mmmmaaaalllllllloooocccc to
  87.      allocate and cccckkkkffffrrrreeeeeeee to free, spurious memory validation errors will occur
  88.      when memory debugging is enabled.  Should you use ffffrrrreeeeeeee to free memory
  89.      allocated by cccckkkkaaaalllllllloooocccc, memory corruption will occur when memory debugging
  90.      is enabled.  Any memory that is to be become the property of the Tcl
  91.      interpreter, such as result space, must be allocated with cccckkkkaaaalllllllloooocccc.  If it
  92.      is absolutely necessary for an application to pass back mmmmaaaalllllllloooocccced memory
  93.      to Tcl, it will work only if Tcl is complied with the TTTTCCCCLLLL____MMMMEEEEMMMM____DDDDEEEEBBBBUUUUGGGG flag
  94.      turned off.  If you convert your application to use this facility, it
  95.      will help you find memory over runs and lost memory.  Note that memory
  96.      allocated by a C library routine requiring freeing should still be freed
  97.      with ffffrrrreeeeeeee, since it calls mmmmaaaalllllllloooocccc rather than cccckkkkaaaalllllllloooocccc to do the
  98.      allocation.
  99.  
  100.      Parmaters:
  101.        oooo _p_t_r - The address of a block to free, as returned by ckalloc.
  102.  
  103.  
  104.    TTTTccccllll____DDDDuuuummmmppppAAAAccccttttiiiivvvveeeeMMMMeeeemmmmoooorrrryyyy
  105.      This function will output a list of all currently allocated memory to the
  106.      specified file.  The following information is outputed for each allocated
  107.      block of memory: starting and ending addresses (excluding guard zone),
  108.      size, source file where cccckkkkaaaalllllllloooocccc was called to allocate the block and line
  109.      number in that file.  It is especially useful to call
  110.      TTTTccccllll____DDDDuuuummmmppppAAAAccccttttiiiivvvveeeeMMMMeeeemmmmoooorrrryyyy after the Tcl interpreter has been deleted.
  111.  
  112.      Parameters:
  113.        oooo _f_i_l_e_N_a_m_e - The name of the file to output the memory list to.
  114.  
  115.    TTTTccccllll____VVVVaaaalllliiiiddddaaaatttteeeeAAAAllllllllMMMMeeeemmmmoooorrrryyyy
  116.      Forces a validation of the guard zones of all currently allocated blocks
  117.      of memory.  Normally validation of a block occures when its freed, unless
  118.      full validation is enabled, in which case validation of all blocks occurs
  119.      when cccckkkkaaaalllllllloooocccc and cccckkkkffffrrrreeeeeeee are called.  This function forces the validation
  120.      to occur at any point.
  121.  
  122.      Parameters:
  123.        oooo _f_i_l_e - The file that this routine is being called from, normally
  124.        ________FFFFIIIILLLLEEEE________.
  125.        oooo _l_i_n_e - The line that this routine is being called from, normally
  126.  
  127.  
  128.  
  129.                                                                         PPPPaaaaggggeeee 2222
  130.  
  131.  
  132.  
  133.  
  134.  
  135.  
  136. MMMMeeeemmmmoooorrrryyyy((((3333TTTTccccllll))))                                                      MMMMeeeemmmmoooorrrryyyy((((3333TTTTccccllll))))
  137.  
  138.  
  139.  
  140.        ________LLLLIIIINNNNEEEE________.
  141.  
  142. EEEENNNNAAAABBBBLLLLIIIINNNNGGGG MMMMEEEEMMMMOOOORRRRYYYY DDDDEEEEBBBBUUUUGGGGGGGGIIIINNNNGGGG
  143.      To enable memory debugging, Tcl should be recompiled from scratch with
  144.      TTTTCCCCLLLL____MMMMEEEEMMMM____DDDDEEEEBBBBUUUUGGGG defined.  This will also compile in a non-stub version of
  145.      TTTTccccllll____IIIInnnniiiittttMMMMeeeemmmmoooorrrryyyy to add the mmmmeeeemmmmoooorrrryyyy command to Tcl.
  146.  
  147.      TTTTCCCCLLLL____MMMMEEEEMMMM____DDDDEEEEBBBBUUUUGGGG must be either left defined for all modules or undefined
  148.      for all modules that are going to be linked together.  If they are not,
  149.      link errors will occur, with either TTTTccccllllDDDDbbbbCCCCkkkkffffrrrreeeeeeee and TTTTccccllll____DDDDbbbbCCCCkkkkaaaalllllllloooocccc or
  150.      TTTTccccllll____CCCCkkkkaaaalllllllloooocccc and TTTTccccllll____CCCCkkkkffffrrrreeeeeeee being undefined.
  151.  
  152. GGGGUUUUAAAARRRRDDDD ZZZZOOOONNNNEEEESSSS
  153.      When memory debugging is enabled, whenever a call to cccckkkkaaaalllllllloooocccc is made,
  154.      slightly more memory than requested is allocated so the memory debugging
  155.      code can keep track of the allocated memory, and also eight-byte ``guard
  156.      zones'' are placed in front of and behind the space that will be returned
  157.      to the caller.  (The size of the guard zone is defined by the C #define
  158.      GGGGUUUUAAAARRRRDDDD____SSSSIIIIZZZZEEEE in _b_a_s_e_l_i_n_e/_s_r_c/_c_k_a_l_l_o_c._c -- it can be extended if you suspect
  159.      large overwrite problems, at some cost in performance.)  A known pattern
  160.      is written into the guard zones and, on a call to cccckkkkffffrrrreeeeeeee, the guard zones
  161.      of the space being freed are checked to see if either zone has been
  162.      modified in any way.  If one has been, the guard bytes and their new
  163.      contents are identified, and a ``low guard failed'' or ``high guard
  164.      failed'' message is issued.  The ``guard failed'' message includes the
  165.      address of the memory packet and the file name and line number of the
  166.      code that called cccckkkkffffrrrreeeeeeee.  This allows you to detect the common sorts of
  167.      one-off problems, where not enough space was allocated to contain the
  168.      data written, for example.
  169.  
  170. TTTTHHHHEEEE MMMMEEEEMMMMOOOORRRRYYYY CCCCOOOOMMMMMMMMAAAANNNNDDDD
  171.      mmmmeeeemmmmoooorrrryyyy _o_p_t_i_o_n_s
  172.           The Tcl mmmmeeeemmmmoooorrrryyyy command gives the Tcl developer control of Tcl's
  173.           memory debugging capabilities.  The memory command has several
  174.           suboptions, which are described below.  It is only available when
  175.           Tcl has been compiled with memory debugging enabled.
  176.  
  177.      mmmmeeeemmmmoooorrrryyyy iiiinnnnffffoooo
  178.           Produces a report containing the total allocations and frees since
  179.           Tcl began, the current packets allocated (the current number of
  180.           calls to cccckkkkaaaalllllllloooocccc not met by a corresponding call to cccckkkkffffrrrreeeeeeee), the
  181.           current bytes allocated, and the maximum number of packets and bytes
  182.           allocated.
  183.  
  184.      mmmmeeeemmmmoooorrrryyyy ttttrrrraaaacccceeee [oooonnnn||||ooooffffffff]
  185.           Turns memory tracing on or off.  When memory tracing is on, every
  186.           call to cccckkkkaaaalllllllloooocccc causes a line of trace information to be written to
  187.           _s_t_d_e_r_r, consisting of the word _c_k_a_l_l_o_c, followed by the address
  188.           returned, the amount of memory allocated, and the C filename and
  189.           line number of the code performing the allocation, for example...
  190.  
  191.              cccckkkkaaaalllllllloooocccc 44440000eeee444477778888 99998888 ttttccccllllPPPPrrrroooocccc....cccc 1111444400006666
  192.  
  193.  
  194.  
  195.                                                                         PPPPaaaaggggeeee 3333
  196.  
  197.  
  198.  
  199.  
  200.  
  201.  
  202. MMMMeeeemmmmoooorrrryyyy((((3333TTTTccccllll))))                                                      MMMMeeeemmmmoooorrrryyyy((((3333TTTTccccllll))))
  203.  
  204.  
  205.  
  206.           Calls to cccckkkkffffrrrreeeeeeee are traced in the same manner, except that the word
  207.           _c_k_a_l_l_o_c is replaced by the word _c_k_f_r_e_e.
  208.  
  209.      mmmmeeeemmmmoooorrrryyyy vvvvaaaalllliiiiddddaaaatttteeee [oooonnnn||||ooooffffffff]
  210.           Turns memory vaidation on or off.  When memory validation is
  211.           enabled, on every call to cccckkkkaaaalllllllloooocccc or cccckkkkffffrrrreeeeeeee, the guard zones are
  212.           checked for every piece of memory currently in existence that was
  213.           allocated by cccckkkkaaaalllllllloooocccc.  This has a large performance impact and
  214.           should only be used when overwrite problems are strongly suspected.
  215.           The advantage of enabling memory validation is that a guard zone
  216.           overwrite can be detected on the first call to cccckkkkaaaalllllllloooocccc or cccckkkkffffrrrreeeeeeee
  217.           after the overwrite occurred, rather than when the specific memory
  218.           with the overwritten guard zone(s) is freed, which may occur long
  219.           after the overwrite occurred.
  220.  
  221.      mmmmeeeemmmmoooorrrryyyy ttttrrrraaaacccceeee____oooonnnn____aaaatttt____mmmmaaaalllllllloooocccc _n_n_n
  222.           Enable memory tracing after _n_n_n cccckkkkaaaallllllllooooccccssss have been performed.  For
  223.           example, if you enter mmmmeeeemmmmoooorrrryyyy ttttrrrraaaacccceeee____oooonnnn____aaaatttt____mmmmaaaalllllllloooocccc 111100000000, after the 100th
  224.           call to cccckkkkaaaalllllllloooocccc, memory trace information will begin being displayed
  225.           for all allocations and frees.  Since there can be a lot of memory
  226.           activity before a problem occurs, judicious use of this option can
  227.           reduce the slowdown caused by tracing (and the amount of trace
  228.           information produced), if you can identify a number of allocations
  229.           that occur before the problem sets in.  The current number of memory
  230.           allocations that have occured since Tcl started is printed on a
  231.           guard zone failure.
  232.  
  233.      mmmmeeeemmmmoooorrrryyyy bbbbrrrreeeeaaaakkkk____oooonnnn____mmmmaaaalllllllloooocccc _n_n_n
  234.           After the nnnnnnnnnnnn allocations have been performed, cccckkkkaaaallllllllooooccccssss output a
  235.           message to this effect and that it is now attempting to enter the C
  236.           debugger.  Tcl will then issue a _S_I_G_I_N_T signal against itself.  If
  237.           you are running Tcl under a C debugger, it should then enter the
  238.           debugger command mode.
  239.  
  240.      mmmmeeeemmmmoooorrrryyyy ddddiiiissssppppllllaaaayyyy _f_i_l_e
  241.           Write a list of all currently allocated memory to the specified
  242.           file.
  243.  
  244. DDDDEEEEBBBBUUUUGGGGGGGGIIIINNNNGGGG DDDDIIIIFFFFFFFFIIIICCCCUUUULLLLTTTT MMMMEEEEMMMMOOOORRRRYYYY CCCCOOOORRRRRRRRUUUUPPPPTTTTIIIIOOOONNNN PPPPRRRROOOOBBBBLLLLEEEEMMMMSSSS
  245.      Normally, Tcl compiled with memory debugging enabled will make it easy to
  246.      isolate a corruption problem.  Turning on memory validation with the
  247.      memory command can help isolate difficult problems.  If you suspect (or
  248.      know) that corruption is occurring before the Tcl interpreter comes up
  249.      far enough for you to issue commands, you can set MMMMEEEEMMMM____VVVVAAAALLLLIIIIDDDDAAAATTTTEEEE define,
  250.      recompile tclCkalloc.c and rebuild Tcl.  This will enable memory
  251.      validation from the first call to cccckkkkaaaalllllllloooocccc, again, at a large performance
  252.      impact.
  253.  
  254.      If you are desperate and validating memory on every call to cccckkkkaaaalllllllloooocccc and
  255.      cccckkkkffffrrrreeeeeeee isn't enough, you can explicitly call TTTTccccllll____VVVVaaaalllliiiiddddaaaatttteeeeAAAAllllllllMMMMeeeemmmmoooorrrryyyy
  256.      directly at any point.  It takes a _c_h_a_r * and an _i_n_t which are normally
  257.      the filename and line number of the caller, but they can actually be
  258.  
  259.  
  260.  
  261.                                                                         PPPPaaaaggggeeee 4444
  262.  
  263.  
  264.  
  265.  
  266.  
  267.  
  268. MMMMeeeemmmmoooorrrryyyy((((3333TTTTccccllll))))                                                      MMMMeeeemmmmoooorrrryyyy((((3333TTTTccccllll))))
  269.  
  270.  
  271.  
  272.      anything you want.  Remember to remove the calls after you find the
  273.      problem.
  274.  
  275. KKKKEEEEYYYYWWWWOOOORRRRDDDDSSSS
  276.      ckalloc, ckfree, free, memory, malloc
  277.  
  278.  
  279.  
  280.  
  281.  
  282.  
  283.  
  284.  
  285.  
  286.  
  287.  
  288.  
  289.  
  290.  
  291.  
  292.  
  293.  
  294.  
  295.  
  296.  
  297.  
  298.  
  299.  
  300.  
  301.  
  302.  
  303.  
  304.  
  305.  
  306.  
  307.  
  308.  
  309.  
  310.  
  311.  
  312.  
  313.  
  314.  
  315.  
  316.  
  317.  
  318.  
  319.  
  320.  
  321.  
  322.  
  323.  
  324.  
  325.  
  326.  
  327.                                                                         PPPPaaaaggggeeee 5555
  328.  
  329.  
  330.  
  331.